package com.youlai.warehouse.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.common.constant.SystemConstants;
import com.youlai.common.util.DateUtils;
import com.youlai.system.model.dto.UserAuthInfo;
import com.youlai.system.model.entity.SysRole;
import com.youlai.system.model.entity.SysUser;
import com.youlai.system.security.util.SecurityUtils;
import com.youlai.warehouse.converter.BasicProductConverter;
import com.youlai.warehouse.mapper.BasicProductMapper;
import com.youlai.warehouse.model.bo.BasicProductBo;
import com.youlai.warehouse.model.entity.BasicProduct;
import com.youlai.warehouse.model.form.BasicProductForm;
import com.youlai.warehouse.model.query.BasicProductPageQuery;
import com.youlai.warehouse.model.vo.BasicProductPageVo;
import com.youlai.warehouse.service.BasicProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class BasicProductServiceImpl extends ServiceImpl<BasicProductMapper, BasicProduct>  implements BasicProductService {

    @Autowired
    private BasicProductMapper basicProductMapper;

    @Autowired
    private BasicProductConverter basicProductConverter;


    @Override
    public IPage<BasicProductPageVo> getBasicProductPage(BasicProductPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<BasicProductBo> page = new Page<>(pageNum, pageSize);

        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");

        // 查询数据
        Page<BasicProductBo> basicProductBoPage = this.basicProductMapper.getBasicProductPage(page, queryParams);
        log.info(basicProductBoPage.getRecords().toString());

//        Page<BasicProductPageVo> pageVo = basicProductConverter.toPageVo(basicProductBoPage);
//        log.info(pageVo.getRecords().toString());

        // 实体转换
        return basicProductConverter.toPageVo(basicProductBoPage);
//        return null;
    }

    @Override
    public boolean saveBasicProduct(BasicProductForm basicProductForm) {
        log.info(basicProductForm.toString());
        String productId = basicProductForm.getProductId();

        long count = this.count(new LambdaQueryWrapper<BasicProduct>().eq(BasicProduct::getProductId, productId));
        Assert.isTrue(count == 0, "产品编码已经存在");

        // 实体转换 form->entity
        BasicProduct entity = basicProductConverter.form2Entity(basicProductForm);

        // 设置添加用户
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setUpdateBy(SecurityUtils.getUserId());

        // 新增产品
        boolean result = this.save(entity);

        return result;
    }

    @Override
    public BasicProductForm getbasicProductForm(Long id) {
        BasicProduct entity = this.getById(id);
        return basicProductConverter.entity2Form(entity);
    }

    @Override
    public boolean  updateBasicProduct (BasicProductForm basicProductForm) {

        long count = this.count(new LambdaQueryWrapper<BasicProduct>()
                .eq(BasicProduct::getProductId, basicProductForm.getProductId())
                .ne(BasicProduct::getId, basicProductForm.getId())
        );
        Assert.isTrue(count == 0, "产品编码已存在");

        // form -> entity
        BasicProduct entity = basicProductConverter.form2Entity(basicProductForm);

        // 修改用户
        boolean result = this.updateById(entity);

        return result;
    }

    @Override
    public boolean deleteBasicProduct(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的产品信息数据为空");
        // 逻辑删除
        List<Long> idsStr = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(idsStr);
    }
}
